/*
 * This is the listener for the send button
 */
package gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JList;
import javax.swing.JTextArea;

/**
 *
 * @author Nana
 */
public class SendListener implements ActionListener{

    private JTextArea msgtoSend;
    private ChatGUI gui;
    private String message ;
    private JList selectedUsers;
    private ArrayList<String> to;
    
    /**
     * The constructor of the listener
     * @param msgtoSend The message to send
     * @param gui The chatGUI
     * @param selectedUsers The list of receivers
     */
    public SendListener(JTextArea msgtoSend, ChatGUI gui, JList selectedUsers ){
        this.msgtoSend = msgtoSend ;
        this.gui = gui ;
        this.selectedUsers = selectedUsers ;
    }
    
    /**
     * The action to perform when clicking on the send button
     * @param e The action event
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        message = msgtoSend.getText();
        msgtoSend.setText("");

        List selection = selectedUsers.getSelectedValuesList();
        to  = new ArrayList<String>(selection.size());
        to.addAll(selection);
        gui.performSendTextMessage(message, to);
    } 
    
}
